var express = require('express');
var router = express.Router();
const passport = require('passport');
var productDAL = require('../DAL/orderDAL')

//创建订单
router.post('/createOrder', passport.authenticate('jwt', { session: false}),function (req, res, next) {
    var order = req.body
    productDAL.createOrder(order, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '创建订单失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '创建订单成功',
                results:results
            })
        }
    })
})
//查看订单
router.get('/getUserOrder', passport.authenticate('jwt', { session: false}),function (req, res, next) {
    var order = req.query
    productDAL.getUserOrder(order, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看订单失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看订单成功',
                results:results
            })
        }
    })
})
//删除订单
// router.post('/delOrder',passport.authenticate('jwt', { session: false}),function (req, res, next) {
//     var order = req.body
//     productDAL.delOrder(order, function (err,results) {
//         if (err) {
//             res.json({
//                 code: 500,
//                 msg: '删除订单失败'
//             })
//         } else {
//             res.json({
//                 code: 200,
//                 msg: '删除订单成功',
//                 results:results
//             })
//         }
//     })
// })
//根据订单id删除订单
router.post('/delOrderById',passport.authenticate('jwt', { session: false}),function (req, res, next) {
    var order = req.body
    productDAL.delOrderById(order, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除订单失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除订单成功',
                results:results
            })
        }
    })
})
//查看订单详情
router.get('/getOrderDetails',passport.authenticate('jwt', { session: false}), function (req, res, next) {
    var order = req.query
    productDAL.getOrderDetails(order, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看订单失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看订单成功',
                results:results
            })
        }
    })
})
//修改订单状态
router.post('/changeOrder', function (req, res, next) {
    var order = req.body
    productDAL.changeOrder(order, function (err,results) {
        if (err) {
            console.log("err",err.message);
            res.json({
                code: 500,
                msg: '修改订单状态失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '修改订单状态成功',
                results:results
            })
        }
    })
})
//删除个人中心订单
router.post('/changePersonalOrder', function (req, res, next) {
    var order = req.body
    productDAL.changePersonalOrder(order, function (err,results) {
        if (err) {
            console.log("err",err.message);
            res.json({
                code: 500,
                msg: '修改订单状态失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '修改订单状态成功',
                results:results
            })
        }
    })
})
//查看已支付订单
router.get('/getPayedUserOrder', passport.authenticate('jwt', { session: false}),function (req, res, next) {
    var order = req.query
    productDAL.getPayedUserOrder(order, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看订单失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看订单成功',
                results:results
            })
        }
    })
})
module.exports = router;